package com.ys.utils.yinsheng.yztdock.util;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Data
@Accessors(chain = true)
public class Result<T> implements Serializable {
    /** 处理成功返回状态码 */
    public static final int CODE_SUCCESS = 0;
    /** 接口调用失败返回状态码 */
    public static final int CODE_FAIL = -1;
    /** 登录失效或未登录 */
    public static final int CODE_OVERDO = -2;

    /** 状态码 0:成功 -1:系统异常 其他请自定义 */
    private int code;
    /** 返回数据体 */
    private T result;
    /** 返回描述 */
    private String message;
    /** 返回其他数据 */
    private Map data = new HashMap<>();

    public static <T> Result<T> newSuccess() {
        Result<T> result = new Result<>();
        result.setCode(CODE_SUCCESS);
        return result;
    }

    public static <T> Result<T> newSuccess(T res) {
        Result<T> result = new Result<>();
        result.setCode(CODE_SUCCESS);
        result.setResult(res);
        return result;
    }

    public static <T> Result<T> newFailure(T res) {
        Result<T> result = new Result<>();
        result.setCode(CODE_FAIL);
        result.setResult(res);
        return result;
    }

    public static <T> Result<T> newFailure(int code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> newException(Exception e) {
        Result<T> result = new Result<>();
        result.setCode(CODE_FAIL);
        result.setMessage(e.getMessage());
        return result;
    }

    public static <T> Result<T> newException(String message) {
        Result<T> result = new Result<>();
        result.setCode(CODE_FAIL);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> newFailure() {
        Result<T> result = new Result<>();
        result.setCode(CODE_FAIL);
        return result;
    }

    public Result appendData(Object property, Object value) {
        this.data.put(property, value);
        return this;
    }
    public Result appendData(Map data) {
        this.data.putAll(data);
        return this;
    }
    
    /**
     * 
     * @Title: success
     * @Description: 判断返回结果是否成功
     * @author Lkx
     * @return boolean
     * @throws
     */
    public boolean success() {
        return code == CODE_SUCCESS;
    }

    /**
     * 
     * @Title: hasObject
     * @Description: 判断返回结果是否有结果对象
     * @author Lkx
     * @return boolean
     * @throws
     */
    public boolean hasObject() {
        return code == CODE_SUCCESS && result != null;
    }
}
